﻿namespace AlgorithmInterview.Chapter04._01;

//找出数组中唯一的重复元素
public class Test04
{
    /// <summary>
    /// 数据映射法
    /// </summary>
    /// <param name="array"></param>
    /// <returns></returns>
    public int FindDup(int[] array)
    {
        if (array == null)
            return -1;
        int len = array.Length;
        int index = 0;
        int i = 0;
        while (true)
        {
            if (array[i] >= len)
                return -1;
            if (array[index] < 0)
                break;
            array[index] *= -1;
            index = -1 * array[index];
            if (index >= len)
            {
                Console.WriteLine("数组中有非法数字");
                return -1;
            }
        }

        return index;
    }
}